﻿Public Class CANYLabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CANYLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-ANY") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CANYLabelState.DoExecute")
        Dim labelType As Integer = -1
        Dim readText As String = task.ProcessText
        Dim itemKey As String = ""
        Dim sysItemCd As String = ""
        Dim orderCd As String = ""
        Dim isInMazda As Boolean = False
        Dim isInInoac As Boolean = False
        Dim uniqueCd As String = ""

        Dim inBoxQty As Integer = 0
        Dim isFullBox As Boolean = True
        Dim groupCd As String = ""




        Dim labelData() As typActionLabelRecord = pdaTask.getGroupCdClean(pdaTask.C_FileCommon.GetLabel(readText))

        If labelData Is Nothing Then
            ''ラベル照合無しエラー
            ''error
            'pdaTask.addWindowLabelBox(labelType, 2)
            pdaTask.rollbackLabel()
            'エラー
            pdaTask.Hardware.warning()
            ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

            SyncLock pdaTask.m_SendLockObject
                Try
                    pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                    pdaTask.C_FileCommon.SendTrxOrder()

                Catch ex As Exception

                End Try
            End SyncLock

            Dim result As String = ""
            Dim prevWindowMode As EnumWindowMode
            Do While result <> "[OK]"
                If result = "" Then
                    prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                    pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_038, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                End If
                result = task.ReadTextWait(-1)
                'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
            Loop
            pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



            task.ErrorCd = "ER"
            task.ProcessText = ""
            nextState = GetNextState(task, GetType(CErrorState))
            Return nextState

        Else
            ''ラベル照合データを取得
            labelType = CCommon.getIntLabelType2(labelData(0).LabelCate)
            sysItemCd = labelData(0).SysitemCd
            uniqueCd = labelData(0).GroupCd
            isInMazda = pdaTask.getGroupCdIsInMazda(labelData)
            isInInoac = pdaTask.getGroupCdIsInInoac(labelData)
            orderCd = labelData(0).OrderCd

            If CCommon.getIntLabelType2(labelData(0).LabelCate) = CCommon.LABEL_DNC And Not CCommon.getIsInMAZDA(labelData(0).LabelData) Then
                Dim itemDAO As CItemDAO = CItemDAO.GetInstance()
                itemKey = CCommon.TrimCode(labelData(0).LabelData, CCommon.EnumGetString.LBL_D_ITEM_KEY)
                Dim itemData As strctItemInfo = itemDAO.GetItemInfo(itemKey)
                inBoxQty = itemData.InBoxQty
            Else
                inBoxQty = labelData(0).ActionQty
            End If


            If labelData(0).LabelState = "F" Then
                isFullBox = True
            Else
                ''端数
                isFullBox = False
            End If
            groupCd = labelData(0).GroupCd




            ''既に読込済みではないかチェック（ユニークコード）グループコードをユニークコードとする

            Dim uniqueCdInfo As CPDATask.strctUniqueCdInfo = Nothing
            uniqueCdInfo.labelType = labelType
            uniqueCdInfo.itemCd = sysItemCd
            uniqueCdInfo.uniqueCd = uniqueCd
            uniqueCdInfo.readDateTime = Now()
            If Not pdaTask.addUniqueCd(uniqueCdInfo) Then
                ''アラート
                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        pdaTask.Hardware.alert()
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_015, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        'Debug.WriteLine(CCommon.CNS_MSG_015)

                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
            Else
                ''出荷指示書対象のラベル群かチェック


                Dim label As New CLabelData()
                label.Id = 0
                label.GroupCd = groupCd
                label.OrderCd = orderCd
                label.SysItemCd = sysItemCd
                label.InBoxQty = inBoxQty
                label.IsFullBox = isFullBox
                'label.ReadText = readText
                label.LabelType = labelType
                'label.LabelRnk = LabelRnk

                label.IsInMazda = isInMazda
                'Debug.WriteLine("label.LabelType==" & label.LabelType)
                'Debug.WriteLine("label.IsInMazda==" & label.IsInMazda)
                'Debug.WriteLine("label.GroupCd==" & label.GroupCd)
                'Debug.WriteLine("label.SysItemCd==" & label.SysItemCd)
                'Debug.WriteLine("label.OrderCd==" & label.OrderCd)
                'Debug.WriteLine("label.InBoxQty==" & label.InBoxQty)
                'Debug.WriteLine("label.IsFullBox==" & label.IsFullBox)
                'Debug.WriteLine("label.ReadText==" & label.ReadText)
                'Debug.WriteLine("label.LabelRnk==" & label.LabelRnk)
                Dim ret As COrderData = pdaTask.C_SHIPORDER.ChkLabelData(label)

                If ret Is Nothing Then
                    ''エラー　対象外のラベル
                    ''error
                    'pdaTask.addWindowLabelBox(labelType, 2)
                    pdaTask.rollbackLabel()
                    'エラー
                    pdaTask.Hardware.warning()
                    ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                    SyncLock pdaTask.m_SendLockObject
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try
                    End SyncLock

                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState))
                    Return nextState
                Else
                    Dim labelState As CPDATask.strctNowLabelState = Nothing
                    labelState.LabelType = labelType
                    labelState.IsInMazda = isInMazda ''DNCラベル読込時、Mの場合はTrue
                    labelState.IsInInoac = isInInoac
                    labelState.GroupCd = groupCd
                    labelState.SysItemCd = sysItemCd
                    labelState.OrderCd = orderCd
                    labelState.OrderQty = 0 ''???????????????
                    labelState.InBoxQty = inBoxQty
                    labelState.IsFullBox = isFullBox
                    labelState.ReadText = readText
                    labelState.LabelRnk = "P"
                    labelState.Begin = True

                    Dim actionLabel() As typActionLabelRecord
                    ReDim actionLabel(labelData.Length - 1)

                    ''送信用ラベル情報をセット----start
                    Dim i As Integer
                    For i = 0 To labelData.Length - 1
                        With actionLabel(i)
                            .GroupCd = labelData(i).GroupCd
                            .ActionDate = labelData(i).ActionDate
                            .ActionTime = labelData(i).ActionTime
                            .LabelCate = labelData(i).LabelCate
                            .LabelRnk = labelData(i).LabelRnk
                            .LabelData = labelData(i).LabelData
                            'Debug.WriteLine("labelData(i).LabelData=" & labelData(i).LabelData)
                            .LabelState = labelData(i).LabelState
                            .ActionQty = labelData(i).ActionQty
                            .EmpCd = labelData(i).EmpCd
                            .ErrorCd = labelData(i).ErrorCd
                        End With
                    Next
                    ''送信用ラベル情報をセット----end

                    If pdaTask.addLabel2(labelState, actionLabel) Then
                        ''正常コミット
                        'Debug.WriteLine("labelState.LabelType==" & labelState.LabelType)
                        'Debug.WriteLine("labelState.IsInMazda==" & labelState.IsInMazda)
                        'Debug.WriteLine("labelState.IsInInoac==" & labelState.IsInInoac)
                        'Debug.WriteLine("labelState.GroupCd==" & labelState.GroupCd)
                        'Debug.WriteLine("labelState.SysItemCd==" & labelState.SysItemCd)
                        'Debug.WriteLine("labelState.OrderCd==" & labelState.OrderCd)
                        'Debug.WriteLine("labelState.OrderQty==" & labelState.OrderQty)
                        'Debug.WriteLine("labelState.InBoxQty==" & labelState.InBoxQty)
                        'Debug.WriteLine("labelState.IsFullBox==" & labelState.IsFullBox)
                        'Debug.WriteLine("labelState.ReadText==" & labelState.ReadText)
                        'Debug.WriteLine("labelState.LabelRnk==" & labelState.LabelRnk)
                        'Debug.WriteLine("labelState.Begin==" & labelState.Begin)
                        pdaTask.commitLabel()
                        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance
                        Dim isBoxEndChk As Boolean = AppManager.GetAppSettings.IsBoxEndChk
                        If isBoxEndChk Then
                            pdaTask.Hardware.nextD()
                        End If

                    Else
                        ''エラー　対象外のラベル
                        ''error
                        'pdaTask.addWindowLabelBox(labelType, 2)
                        pdaTask.rollbackLabel()
                        'エラー
                        pdaTask.Hardware.warning()
                        ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                        SyncLock pdaTask.m_SendLockObject
                            Try
                                pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                                pdaTask.C_FileCommon.SendTrxOrder()

                            Catch ex As Exception

                            End Try
                        End SyncLock

                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            End If
                            result = task.ReadTextWait(-1)
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                        task.ErrorCd = "ER"
                        task.ProcessText = ""
                        nextState = GetNextState(task, GetType(CErrorState))
                        Return nextState
                    End If

                End If

            End If


        End If





        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CANYLabelState.DoExit")
        Return True
    End Function

End Class
